/**
 * Created by kangli on 2018/4/8.
 */
var sum;
var counts='';
var pageThis =0;
var pagecount = 8;
var index = 0;
var masterLists='';
var teacherId=''
$(function(){
    getMasterList();
});
//得到名师工作室列表
function getMasterList(){
    var  data={page: index ,size: pagecount};
    console.log(data);
    AjaxSubmit("GET",data, basePath + "/super/common/famousTeacher/getFamousTeacher",getFamousTeacher_fun);
}

function getFamousTeacher_fun(res){
    console.log(res);
    if(res.status=='SUCCESS'){
        masterLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if (masterLists.length != 0) {
            $("#masterPage").css("display","block");
            if (sum % pagecount == 0) {
                counts = parseInt(sum / pagecount);
            }
            else {
                counts = parseInt(sum / pagecount) + 1;
            }
            $("#masterPage").paging({
                pageNo: pageThis,
                totalPage: counts,
                totalSize: sum,
                callback: function (num) {
                    console.log(num);
                    index = num - 1;
                    getMasterList();
                    pageThis = num;
                }
            });
            $(".masterTbody").empty();
            $(".warnDiv7").remove();
            $.each(masterLists, function (i, masterList) {
                var data1 = '{"teacherId":"'+masterList.teacherId+'"}';
                console.log(data1);
                AjaxSubmit("GET",JSON.parse(data1), basePath + "/super/common/famousTeacher/getTeacherById",getTeacherById_fun);
                function getTeacherById_fun(res){
                    console.log(res)
                   teacherId  = res;
                    if(res.status=='SUCCESS'){
                        var positional = '';
                        if(res.obj.positional=='PROFESSOR'){
                            positional='教授'
                        }else if(res.obj.positional=='ASSOCIATE_PROFESSOR'){
                            positional='副教授'
                        }else if(res.obj.positional=='LECTURER'){
                            positional='讲师'
                        }
                        var researchDirection = res.obj.researchDirection || '';
                        var researchFindings = res.obj.researchFindings || '';
                        var graduateSchool = res.obj.graduateSchool || '';
                        var html = '  <tr> ' +
                            '<td>'+res.obj.teacherName+'</td> ' +
                            '<td>'+graduateSchool+'</td> ' +
                            '<td>'+positional+'</td> ' +
                            '<td>'+researchDirection+'</td> ' +
                            '<td>'+researchFindings+' </td> ' +
                            '<td class="texC colorBlue">  <span class="texC  cursorP" data-target="#myModalteachermes" onclick="showteacher(this)">查看</span></td> </tr>';
                        $(".masterTbody").append(html)
                    }
                }
            });
        }
        else{
            $("#masterPage").css("display","none");
            $(".masterTbody").empty();
            $(".warnDiv7").remove();
            var html = $('<div class="text-center warnDiv7 "  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
            $(".masterTable").after(html)
        }
    }

}
//新增名师
function addFamousTeacher(btn){
    $("#addMaster").modal('show');
    var  data={page: index ,size: 100000};
    AjaxSubmit("GET",data, basePath + "/super/common/famousTeacher/getTeacher",getTeacher_fun);
    function getTeacher_fun(res){
        console.log(res)
        if(res.status=='SUCCESS') {
            var teachLists = res.obj.content;
            if (teachLists.length != 0) {
                $.each(teachLists, function (i, teachList) {
                    $(".teacherSelect").append('<option data-id='+teachList.id+'>'+teachList.teacherName+'</option>');
                })
            }else{
                $(".teacherSelect").append('<option>无</option>');
            }
        }
    }
}
//点击保存名师
function saveFamousTeacher(btn){
    $("#addMaster").modal('hide');
     var teachId = $(".teacherSelect").children("option:selected").attr("data-id");
    var brief = $(".brief").val();
    var  data={teacherId: teachId,brief: brief };
    AjaxSubmit("POST",data, basePath + "/super/common/famousTeacher/addFamousTeacher",addFamousTeacher_fun);
    function addFamousTeacher_fun(res){
        console.log(res);
        if(res.status=='SUCCESS'){
            confirm("添加成功！");
            index = $(".pagination li").find(".active").find("a").text();
            getMasterList();
        }
    }
}

//看教师信息
function showteacher(btn){
    $("#myModalteachermes").modal('show');
    if(teacherId.obj!=null&&teacherId.obj!=undefined&&teacherId.obj!=""){
        var sex='';
        var school=teacherId.obj.graduateSchool || '';
        var birthday=changeTime(teacherId.obj.birthday) || '';
        var schoolyear = teacherId.obj.schoolyear ||'';
        var positional='';
        var duties='';
        if(teacherId.obj.sex=='M'){
            sex='男'
        }else{
            sex='女'
        }

        /*职称分批*/
        if(teacherId.obj.positional=="LECTURER"){
            positional='讲师'
        }else   if(teacherId.obj.positional=="ASSOCIATE_PROFESSOR"){
            positional='副教授'
        }else   if(teacherId.obj.positional=="PROFESSOR"){
            positional='教授'
        }
        /*学历分批*/
        var education='';
        if(teacherId.obj.education=="JUNIOR_COLLEGE_STUDENT"){
            education='专科'
        }else   if(teacherId.obj.education=="UNDERGRADUATE"){
            education='本科'
        }else   if(teacherId.obj.education=="POSTGRADUATE"){
            education='硕士'
        }else   if(teacherId.obj.education=="DOCTOR"){
            education='博士'
        }else   if(teacherId.obj.education=="POST_DOCTORAL"){
            education='博士后'
        }
        /* 职务分批*/
        if(teacherId.obj.duties=="YUAN_ZHANG("){
            duties='院长'
        }else   if(teacherId.obj.duties=="FU_YUAN_ZHANG"){
            duties='副院长'
        }else   if(teacherId.obj.duties=="SHU_JI"){
            duties='书记'
        }else   if(teacherId.obj.duties=="FU_SHU_JI"){
            duties='副书记'
        }else   if(teacherId.obj.duties=="KE_YAN_SHI_ZHU_REN"){
            duties='科研室主任'
        }else   if(teacherId.obj.duties=="XUE_KE_DAI_TOU_REN"){
            duties='学科带头人'
        }else   if(teacherId.obj.duties=="IDEOLOGICAL_POLITICAL"){
            duties='思政老师'
        }

        var researchDirection=teacherId.obj.researchDirection ||'';
        var awardWinning=teacherId.obj.awardWinning ||'';
        var phone=teacherId.obj.phone ||'';
        var email=teacherId.obj.email ||'';
        var teacherName=teacherId.obj.teacherName ||'';
        var representativeWorksPapers=teacherId.obj.representativeWorksPapers ||'';
        var schoolName=teacherId.obj.schoolName ||'';
        $(".teaname").text(teacherName);
        $(".teaxueli").text(education);
        $(".teaschool").text(schoolName);
        $(".teaxuewei").text(education);
        $(".teasex").text(sex);
        $(".tealastschool").text(school);
        $(".teabirthday").text(birthday);
        $(".teakeshi").text(schoolyear);
        $(".teazc").text(positional);
        $(".teazw").text(duties);
        $(".teadirect").text(researchDirection);
        $(".teahoror").text(awardWinning);
        $(".teadaibiao").text(representativeWorksPapers);
        $(".teaphone").text(phone);
        $(".teamail").text(email);
    }
}